<?php
    /* functions */
    function checkTelcoName($mobile)
    {        
        $first = substr($mobile, 0, 1);
        if($first=="0"){
            $mobile = substr($mobile, 1, strlen($mobile) - 1);
        } else if($first="8"){
            $mobile = substr($mobile, 2, strlen($mobile) - 2);
        }
        
        $mobile = "84" . $mobile;
        
        $patternViettel = "/^84(9[678]|16[2-9])[\d]{7}/";
        $patternMobi = "/^84(9[03]|12[01268])[\d]{7}/";
        $patternVina = "/^84(9[14]|12[34579])[\d]{7}/";
        $patternVnmobile = "/^84(9[2]|18[86])[\d]{7}/";
        $patternBeeline = "/^84(9[9]|19[9])[\d]{7}/";
        
        if(preg_match($patternViettel, $mobile)){
            $telco = "VTT";    
        } else if(preg_match($patternMobi, $mobile)){
            $telco = "VMS";
        } else if(preg_match($patternVina, $mobile)){
            $telco = "VNP";
        } else if(preg_match($patternVnmobile, $mobile)){
            $telco = "VNM";
        } else if(preg_match($patternBeeline, $mobile)){
            $telco = "BEE";
        } else {
            $telco = "";
        }
        
        return $telco;    
    }
    
    function service($data)
    {        
        $privateKey = "RMdasGJYI6EmmtK";
        $password = md5($data["transactionId"] . $data["sender"] . $data["serviceCode"] . $data["content"] . $privateKey);
        $route = checkTelcoMeCorp($data["sender"]);
        
        $url = "http://pay.gomobi.vn/index.php/sms/sms/partner/taoviec";
        $url .= "?username=" . urlencode("taoviec");
        $url .= "&password=" . urlencode($password);
        $url .= "&smsid=" . urlencode($data["transactionId"]);
        $url .= "&sender=" . urlencode($data["sender"]);
        $url .= "&receiver=" . urlencode($data["serviceCode"]);
        $url .= "&content=" . urlencode($data["content"]);                
        $url .= "&route=" . urlencode($route);
        $url .= "&timereceived=" . urlencode(date("Y-m-d H:i:s"));
        $url .= "&data=" . urlencode("json");
        
        $response = cUrl($url);
        $response = json_decode(utf8_decode($response));
        return $response;                            
    } 
    
    function buildSyntax($content, $regx)
    {
        preg_match($regx, $content, $match);
        
        
        $action = isset($match[2]) ? $match[2] : "";
        $game_user = isset($match[4]) ? $match[4] : "";
        
        /* Lấy userId */
        $user = getUserLtbc($game_user);
        
        $syntax = array();
        $syntax["userId"] = !empty($user["userId"]) ? $user["userId"] : 0;
        $syntax["action"] = "";
        if($action=="KH"){
            $syntax["action"] = "reg";
        } else if($action=="NGOC"){
            $syntax["action"] = "nap";
        }
        
        return $syntax;   
    }
    
    function getUserLtbc($game_user)
    {
        $sql = "SELECT userId FROM c_ltbc WHERE game_user='" . mysql_escape_string($game_user) . "'";
        $result = mysql_query($sql);
        $rows = mysql_fetch_assoc($result);
        return $rows;
    }

    /*
    * processing
    */
    
    function processing($data)
    {       
        $ltbc_id = 174;
        
        $regx = "/^TVA LTBC(\s+)(KH|NGOC)(\s+)([a-zA-Z0-9.]{1,})$/si";                        
        
        if(preg_match($regx_active, $data["content"]) || preg_match($regx_nap, $data["content"])){
            
            /* active user */
            $syntax = buildSyntax($data["content"], $regx);
            $list_service_code = array("6522", "6722", "6622");
            $data["id"] = $ltbc_id;
            $data["action"] = $syntax["action"];
            $data["userId"] = $syntax["userId"];
            
            if(in_array($data["serviceCode"], $list_service_code)){
                                                        
                $result = service($data);                        
                                       
                $data["status"] = $result->cdr==1 ? 1 : 0;
                $response["status"] = $result->cdr;
                $response["msg"] = $result->mt;
                
            } else {
                $response["status"] = 3;                        
                $response["msg"] = "Tin nhan khong dung dau so quy dinh";                
                
                $data["status"] = 0;                 
            }
            
        } else {
            $response["status"] = 2;                
            $response["msg"] = "Tin nhan sai cu phap";                
            
            $data["userId"] = 0;                
            $data["status"] = 0;    
            
            $data["id"] = 0; //ID game
        } 
        
        /* log sms */
        $data["telco"] = checkTelco($data["sender"]);    
        $data["price"] = priceServiceCode($data["serviceCode"]);    
        $data["partnerId"] = 60; //ID partner RS MOBILE    
        $data["type"] = 1; //Thể loại game   
        
        $data["output"] = $response["status"] . "|" . $data["transactionId"] . "|" . $response["msg"];    
        
        /* tỷ lệ ăn chia */
        $percent = getPercent($data["id"]);
        
        $data["percent_wapmaster"] = isset($percent["percent_wapmaster"]) ? $percent["percent_wapmaster"] : 0;
        $data["percent_partner"] = isset($percent["percent_partner"]) ? $percent["percent_partner"] : 0;
        
        $result = InsertSmsLog($data);   
        return $response;   
    }
?>